package com.ljb.readaloud.ui;

import android.content.Context;
import android.os.Environment;
import android.os.Handler;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;

import com.czt.mp3recorder.MP3Recorder;

import java.io.File;

/**
 * ================================================
 * 作    者：JayGoo
 * 版    本：
 * 创建日期：2017/7/18
 * 描    述:
 * ================================================
 */
public class WaveMp3Recorder extends WaveLineView {
    private static final String TAG = "WaveMp3Recorder";
    private static final int UPDATE_TIME = 200;
    private MP3Recorder mp3Recorder;
    private String recordFileDir;
    private String recordFileName;

    public WaveMp3Recorder(Context context) {
        this(context, null);
    }

    public WaveMp3Recorder(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public WaveMp3Recorder(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void updateRecordingUI() {
        if (mp3Recorder != null)
            setVolume(100 * mp3Recorder.getVolume() / mp3Recorder.getMaxVolume());
    }

    private void updateStopRecordUI() {
        stopAnim();
    }

    private void updateStartRecordUI() {
        startAnim();
    }

    /**
     * 录音更新进度条
     */
    private Handler mHandler = new Handler();
    private Runnable mRecordProgressTask = new Runnable() {
        public void run() {
            //录音停止,动画停止
            if (!isRecording()) {
                stopRecord(true);
            } else {
                updateRecordingUI();
                if (mHandler != null)
                    mHandler.postDelayed(mRecordProgressTask, UPDATE_TIME);
            }
        }
    };

    /**
     * 初始化Recorder
     */
    public WaveMp3Recorder initRecorder(String filePath, String fileName) {
        if (TextUtils.isEmpty(filePath) || TextUtils.isEmpty(fileName))
            Log.e(TAG, "initRecorder: 文件路径,文件名不能为空");

        recordFileDir = filePath;
        recordFileName = fileName;
        Log.e(TAG, "initRecorder: path:" + getRecordFilePath());

        File recordFile = new File(recordFileDir, recordFileName);
        mp3Recorder = new MP3Recorder(recordFile);
        mp3Recorder.setDefaultLameMp3BitRate(96);
        return this;
    }


    /**
     * 停止录音和动画
     */
    public void stopRecord(boolean isFromUser) {
        if (mHandler != null)
            mHandler.removeCallbacksAndMessages(null);
        try {
            updateStopRecordUI();
            if (mp3Recorder != null)
                mp3Recorder.stop();
            Log.e(TAG, "stopRecord: ");
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mOnRecordStateChangeListener != null)
            mOnRecordStateChangeListener.onStopRecord(getRecordFile(), isFromUser);
    }

    /**
     * 停止录音和动画(非用户主动停止)
     */
    public void stopRecord() {
        stopRecord(false);
    }

    /**
     * 开始录音和动画
     */
    public void startRecord() {
        updateStartRecordUI();
        try {
            if (mp3Recorder != null)
                mp3Recorder.start();
            Log.e(TAG, "startRecord: ");
            if (mHandler != null) {
                mHandler.post(mRecordProgressTask);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (mOnRecordStateChangeListener != null)
            mOnRecordStateChangeListener.onStartRecord();
    }

    /**
     * 判断是否为正在录音状态
     *
     * @return
     */
    public boolean isRecording() {
        return mp3Recorder != null && mp3Recorder.isRecording();
    }

    /**
     * 设置最长录音时间
     *
     * @param millis
     */
    public WaveMp3Recorder setMaxRecordTime(long millis) {
        if (mp3Recorder != null)
            mp3Recorder.setMaxRecordTime(millis);
        return this;
    }

    public String getRecordFileName() {
        return recordFileName;
    }

    /**
     * 获取录音文件
     *
     * @return
     */
    public File getRecordFile() {
        return new File(recordFileDir, recordFileName);
    }

    /**
     * 获取录音文件路径
     *
     * @return
     */
    public String getRecordFilePath() {
        return recordFileDir + "/" + recordFileName;
    }

    /**
     * 获取MP3Recorder对象
     *
     * @return
     */
    public MP3Recorder getMp3Recorder() {
        return mp3Recorder;
    }


    /**
     * 录音状态监听
     */
    private OnRecordStateChangeListener mOnRecordStateChangeListener;

    public interface OnRecordStateChangeListener {
        void onStartRecord();

        void onStopRecord(File recordFile, boolean isFromUser);
    }

    public WaveMp3Recorder setOnRecordStateChangeListener(OnRecordStateChangeListener listener) {
        mOnRecordStateChangeListener = listener;
        return this;
    }
}
